package com.du.fileload.framework.utils;

import com.du.plugin.common.tool.StringHelper;

import java.text.DecimalFormat;
import java.util.Locale;

public class DiskCapacityUtils {

    /**
     * 字节转kb mb...
     * @param bytes
     * @return
     */
    public static String getNetFileSizeDescription(long bytes) {
        if (bytes == 0) return "0B";
        final String[] units = new String[]{"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
        int digitGroups = (int) (Math.log10(bytes) / Math.log10(1024));
        return new DecimalFormat("#,##0.##").format(bytes / Math.pow(1024, digitGroups)) + "" + units[digitGroups];
    }


    /**
     * kb转字节长度   默认kb
     * @param lenStr 128kb=128*1024
     * @return long
     */
    public static long getFileLen(String lenStr) {

        String lenSizeStr=StringHelper.get_StringNum(lenStr);
        if(lenSizeStr.trim().length()==0)
            return 0l;

        String sunit=lenStr.replace(lenSizeStr,"").toLowerCase(Locale.ROOT);
        Long lenSize=Long.parseLong(lenSizeStr);
        switch (sunit){
            case "k":
            case "kb":
                lenSize=lenSize*1024;
                break;
            case "m":
            case "mb":
                lenSize=lenSize*1024*1024;
                break;
            case "g":
            case "gb":
                lenSize=lenSize*1024*1024*124;
                break;
            default:
                lenSize=lenSize*1024;
        }
        return lenSize;
    }
}
